﻿
using System.Web.Mvc;
using Dexter.Dependency;
using Dexter.Web.Mvc.ViewModel.ViewsData;

namespace Dexter.PluginManager {
	public class PluginFilter : ActionFilterAttribute {
		readonly IRenderPluginManager pluginManager;

		/// <summary>
		/// Initializes a new instance of the <see cref="T:System.Web.Mvc.ActionFilterAttribute"/> class.
		/// </summary>
		public PluginFilter ( ) {
			pluginManager = DexterContainer.Resolve <IRenderPluginManager> ( );
		}

		public override void OnActionExecuted ( ActionExecutedContext filterContext ) {
			var viewResult = filterContext.Result as ViewResult;

			if(viewResult == null) {
				base.OnActionExecuted ( filterContext );
				return;
			}

			var model = viewResult.Model as PublicBaseViewData;

			if (model != null) {
				pluginManager.ActionExecuted ( filterContext.HttpContext , model );
			}

			base.OnActionExecuted ( filterContext );
		}
	}
}
